package com.company.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.project.command.supplier.SupplierCreateOrUpdateCommand;
import com.company.project.command.supplier.SupplierListCommand;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.supplier.Supplier;
import com.company.project.service.HttpSessionService;
import com.company.project.service.supplier.SupplierService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequestMapping("/supplier")
@RestController
@Api(tags = "供应商管理")
public class SupplierController {


    @Autowired
    private SupplierService supplierService;
    @Autowired
    private HttpSessionService sessionService;

    @PostMapping("/create")
    public DataResult create(@Valid @RequestBody SupplierCreateOrUpdateCommand command) {
        command.setOwner(sessionService.getCurrentUserId());
        command.setCreatedBy(sessionService.getCurrentUsername());
        this.supplierService.create(command);
        return DataResult.success();
    }

    /**
     * @param command
     * @return
     */
    @PostMapping("/list")
    public DataResult list(@RequestBody SupplierListCommand command){
        command.setOwner(sessionService.getCurrentUserId());
        IPage<Supplier> pagingResult = this.supplierService.selectAllTables(command);
        return DataResult.success(pagingResult);
    }


    /**
     * 更新
     *
     * @param command
     * @return
     */
    @PostMapping("/{id}/update")
    public DataResult update(@PathVariable("id") String id, @Valid @RequestBody SupplierCreateOrUpdateCommand command, BindingResult bindingResult) {
        command.setOwner(sessionService.getCurrentUserId());
        command.setCreatedBy(sessionService.getCurrentUsername());
        command.validData(bindingResult);
        this.supplierService.update(id, command);
        return DataResult.success();

    }


    /**
     * 启用
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/enable")
    public DataResult enable(@PathVariable("id") String id) {
        this.supplierService.enable(id, sessionService.getCurrentUsername());
        return DataResult.success();
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/disable")
    public DataResult disable(@PathVariable("id") String id) {
        this.supplierService.disable(id, sessionService.getCurrentUsername());
        return DataResult.success();
    }

    /**
     * 获取
     */
    @GetMapping("/get/{id}")
    public DataResult get(@PathVariable("id") String id) {
        return DataResult.success(this.supplierService.get(id));
    }

    /**
     * 获取全部
     */
    @PostMapping("/findAll")
    public DataResult findAll() {
        return DataResult.success(this.supplierService.findAll());
    }

    /**
     * 获取启用的
     */
    @PostMapping("/get-option")
    public DataResult getOption() {
        return DataResult.success(this.supplierService.findByEnable(sessionService.getCurrentUserId()));
    }

    /**
     * 获取供应商
     */
    @PostMapping("/batch-get")
    public DataResult batchGet(@RequestBody List<String> ids) {
        return DataResult.success(this.supplierService.findByIds(ids));
    }
    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id){
        this.supplierService.removeById(id);
        return DataResult.success();
    }
}
